<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<script type="text/javascript">
		//深拷贝
		// let obj = {
		// 	name: 'xiao',
		// 	age: 20
		// }
		// function clone(obj){
		// 	if(!obj && typeof obj !== 'object') return;
		// 	let newObj = obj.constructor === Object?{}:[];
		// 	for(var key in obj){
		// 		if(obj.hasOwnProperty(key)){
		// 			newObj[key] = (obj[key] && typeof obj[key] === 'object')? clone(obj[key]) : obj[key];
		// 		}
		// 	}
		// 	return newObj;
		// }
	</script>
	<script type="text/javascript">
		// Create Phone Number

		function createPhoneNumber(numbers){
		    var format = "(xxx) xxx-xxxx";
		    for(var i = 0; i < numbers.length; i++){
		        format = format.replace('x', numbers[i]);
		    }
		    return format;
		}
	</script>
	<script type="text/javascript">
		// Find the odd int
		var xs = [1,2,3,4,3,2,1]
		const findOdd = (xs) => xs.reduce((a, b) => a ^ b);
		//console.log(findOdd(xs))
	</script>
	<script type="text/javascript">
		// likes [] // must be "no one likes this"
		// likes ["Peter"] // must be "Peter likes this"
		// likes ["Jacob", "Alex"] // must be "Jacob and Alex like this"
		// likes ["Max", "John", "Mark"] // must be "Max, John and Mark like this"
		// likes ["Alex", "Jacob", "Mark", "Max"] // must be "Alex, Jacob and 2 others like this"
		var names = ["Alex", "Jacob", "Mark", "Max"];
		function likes (names) {
		    var tpl = [
		        'no one likes this',
		        '{name} likes this',
		        '{name} and {name} like this',
		        '{name}, {name} and {name} like this',
		        '{name}, {name} and {n} others like this'
		    ];
		    var idx = Math.min(names.length, 4);
		    
		    return tpl[idx].replace(/{name}|{n}/g, function (val) {
		        return val === '{name}' ? names.shift() : names.length;
		    });
		}
		//console.log(likes(names))
	</script>
	<script type="text/javascript">
		// Shortest Word

		function findShort(s){
		    return Math.min.apply(null, s.split(' ').map(w => w.length));
		}
		function findShort2(s) {
		  return s.split(' ').map(w => w.length).reduce((a, b) => a > b ? b : a, Number.MAX_VALUE)
		}
		//console.log(findShort2('wo shi yige sasdf'))
	</script>
	<script type="text/javascript">
		// Sum of Digits / Digital Root
		// 题目：创建一个计算digital root的函数
		// digital root是数字中各位数字的递归总和。给定n，取n各位数字之和。如果该值是两位数或者更多，则继续以这种方式递归，直到产生一位数字，这个数字就是digital root。这只适用于自然数。
		// Example:
		// digital_root(16)
		// => 1 + 6
		// => 7

		// digital_root(942)
		// => 9 + 4 + 2
		// => 15 ...
		// => 1 + 5
		// => 6

		// digital_root(132189)
		// => 1 + 3 + 2 + 1 + 8 + 9
		// => 24 ...
		// => 2 + 4
		// => 6

		// 莽夫解法：
		// function digital_root(n) {
		//     let num = n;
		//     if (num < 10){
		//         return num
		//     }else {
		//         return arguments.callee((num+'').split('').reduce(function (a,b) {
		//             return parseInt(a) + parseInt(b)
		//         }))
		//     }
		// }
		// 优雅
		function digital_root(n) {
		    return (n - 1) % 9 + 1;
		}
		//console.log(digital_root())
	</script>
	<script type="text/javascript">
		var data = {
			name: 'liusxi',
			age: 14
		}
		var arr = []
		for(var key in data){
			arr.push(key+'='+data[key])
		}
		console.log(arr.join('&'))
	</script>
</body>
</html>